const express = require('express')
const router = express.Router()
const jwt = require('jsonwebtoken');
var secret = 'delicacies'
const mysql = require("../mysql/config")
let connection = null
const resSend = require('../utils/resSend')
router.post('/register', async (req, res) => {
    let { email, pwd, name } = req.body
    connection = mysql.createConnection()
    connection.connect()
    connection.query("SELECT * FROM user_table WHERE uEmail=?", req.body.email, async (err, result) => {
        if (err) {
            resSend(res, 500, err.message || "服务异常")
        } else {
            if (result.length > 0) {
                resSend(res, 500, "用户已存在！")
            } else {
                connection.query(`INSERT INTO user_table (uEmail, uPwd,uImage,uName) VALUES (?,?,?,?)`, [email, pwd, `https://q1.qlogo.cn/g?b=qq&nk=${email}&s=140`, name], function (error, results, fields) {
                    if (error) {
                        resSend(res, 500, err.message || "服务异常")
                    } else {
                        resSend(res, 200, "操作成功！")
                    }
                })
            }
        }
    })
})

// 用户登录
router.post('/login', async (req, res) => {
    if (req.body.email && req.body.pwd) {
        connection = mysql.createConnection()
        connection.connect()
        connection.query("SELECT * FROM user_table WHERE uEmail=? AND uPwd=?", [req.body.email, req.body.pwd], async (err, result) => {
            if (err) {
                resSend(res, 500, "服务异常")
            } else {
                if (result.length > 0) {
                    if (result[0].isDel != 1) {
                        resSend(res, 500, "账号已经被删除，请联系管理员操作！")
                    } else {
                        let token = jwt.sign({ email: req.body.email, id: result[0].id, role: result[0].role }, secret);
                        resSend(res, 200, "操作成功！", token)
                    }
                } else {
                    resSend(res, 500, "用户不存在，请联系管理员操作！")
                }
            }
        })
    } else {
        resSend(res, 400, "请输入邮箱号和密码！")
    }
})

// 获取所有文章
router.get('/getAllArticle', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query("SELECT * FROM article_table where goodsId!=0", (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常")
        } else {
            if (result.length > 0) {
                result.forEach(val => {
                    val.imageList = JSON.parse(JSON.parse(val.imageList))
                })
            }
            resSend(res, 200, "操作成功！", result)
        }
    })
})

// 获取所有商品
router.get('/getAllGoods', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query("SELECT * FROM article_table where goodsId=0", (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常")
        } else {
            if (result.length > 0) {
                result.forEach(val => {
                    val.imageList = JSON.parse(val.imageList)
                })
            }
            resSend(res, 200, "操作成功！", result)
        }
    })
})

// 获取所有广告
router.get('/getAllAd', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query("SELECT * FROM advertisement_table", (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常")
        } else {
            if (result.length > 0) {
                result.forEach(val => {
                    val.imageList = JSON.parse(val.imageList)
                })
            }
            resSend(res, 200, "操作成功！", result)
        }
    })
})

// 获取所有优惠券
router.get('/getAllCoupon', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query("SELECT * FROM coupon_table", (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常")
        } else {
            resSend(res, 200, "操作成功！", result)
        }
    })
})

// 获取所有意见
router.get('/getAllFeedback', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query("SELECT * FROM opinion_table", (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常")
        } else {
            resSend(res, 200, "操作成功！", result)
        }
    })
})

module.exports = router